package cj.web.sc.controllers;

import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import net.paoding.rose.web.annotation.Param;
import net.paoding.rose.web.annotation.Path;
import net.paoding.rose.web.annotation.rest.Get;
import net.paoding.rose.web.annotation.rest.Post;
import net.paoding.rose.web.var.Model;
import net.sf.json.JSONObject;

import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;

import cj.utils.Constants;
import cj.web.constants.MemuEnum;
import cj.web.login.controllers.BaseController;
import cj.web.sc.dao.SCCategoryDAO;
import cj.web.sc.domain.SCCategory;

@Path("/category")
public class SCCategoryController extends BaseController{

	private static Logger logger = Logger.getLogger(SCCategoryController.class.getName());
	
	@Autowired
	private SCCategoryDAO categoryDao;

	@Get("show")
	public String show(Model model,SCCategory sCCategory)
	{
		model.add("MemuEnum", MemuEnum.sccategory);
		return "/views/sc/categoryList.vm";
	}
	
	@Post("list")
	public String list(Model model,SCCategory category)
	{
		List<Map> objs = categoryDao.queryByName(category);
		int size = categoryDao.sizeByName(category);
		
		JSONObject jo = new JSONObject();
		jo.put("data", objs);
		jo.put("draw", category.getDraw());
		jo.put("recordsTotal", size);
		jo.put("recordsFiltered", size);
		return "@" + jo.toString();
	}
	
	@Get("create")
	public String create(Model model, @Param("parentId")int parentId)
	{
		model.add("parentId",parentId);
		model.add("MemuEnum", MemuEnum.sccategory);
		return "/views/sc/categoryDetail.vm";
	}
	
	@Post("crtCategory")
	public String crtCategory(Model model,SCCategory category, HttpServletRequest request){
		JSONObject jo = new JSONObject();
		jo.put("returnFlag", Constants.returnObj.returnFlag_true);
		
		String acount = returnSessionObj(request).getAccount();
		category.setCreateUser(acount);
		category.setUpdateUser(acount);
		category.setDeleteFlag(1);
		
		try {
			categoryDao.create(category);
		} catch (Exception e) {
			jo.put("returnFlag", Constants.returnObj.returnFlag_flase);
		}
		
		return "@" + jo.toString();
	}
	
	@Get("edit")
	public String edit(Model model, @Param("id") int id, HttpServletRequest request)
	{
		SCCategory obj = categoryDao.queryById(id);
		model.add("obj", obj);
		model.add("MemuEnum", MemuEnum.sccategory);
		return "/views/sc/categoryEdit.vm";
	}
	
	@Post("update")
	public String update(Model model,SCCategory category, HttpServletRequest request)
	{
		JSONObject jo = new JSONObject();
		jo.put("returnFlag", Constants.returnObj.returnFlag_true);
		
		String acount = returnSessionObj(request).getAccount();
		category.setUpdateUser(acount);
		
		try {
			categoryDao.update(category);
		} catch (Exception e) {
			jo.put("returnFlag", Constants.returnObj.returnFlag_flase);
		}
		return "@" + jo.toString();
	}
	
	@Get("deleteById")
	public String deleteById(Model model,@Param("id") int id, HttpServletRequest request)
	{
		SCCategory category = categoryDao.queryById(id);
		if(category != null){
			String acount = returnSessionObj(request).getAccount();
			
			category.setUpdateUser(acount);
			categoryDao.deleteById(category);
		}
		
		return "r:show";
	}
}
